//
//  InteractiveCardView.swift
//  InteractiveCard
//
//  Created by tianqi on 2017/11/10.
//  Copyright © 2017年 david.com. All rights reserved.
//

import UIKit

private let KMaxAmDistance: CGFloat = 200
private let KMaxAmAngle: CGFloat = CGFloat(Double.pi / 5)
private let KMaxAmScale: CGFloat = 0.2


class InteractiveCardView: UIView {
    
    var _imageView: UIImageView!
    var _blurView:UIVisualEffectView!
    var _originPoint: CGPoint!
  
    init(img: UIImage, frame: CGRect) {
        super.init(frame: frame)
        
        _blurView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.dark))
        _blurView.frame = bounds
        addSubview(_blurView)
        
        _imageView = UIImageView(image: img)
        _imageView.center = self.center
        _imageView.bounds = CGRect(x: 0, y: 0, width: 200, height: 150)
        _imageView.layer.cornerRadius = 10
        _imageView.layer.masksToBounds = true
        addSubview(_imageView)

        let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction))
        self.addGestureRecognizer(pan)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension InteractiveCardView {
    
    
    @objc func panAction(gesture: UIPanGestureRecognizer) {
        
        
        let translation = gesture.translation(in: self)
        
        switch gesture.state {
        case .began:
            _originPoint = _imageView.center
        case .changed:
            // 1.平移
            _imageView.center = CGPoint(x: _originPoint.x, y: _originPoint.y + translation.y)
            
            let Y = min(KMaxAmDistance, abs(translation.y))
            
            //一个开口向下,顶点(SCROLLDISTANCE/2,1),过(0,0),(SCROLLDISTANCE,0)的二次函数
            let factorScroll = abs( -1 / (KMaxAmDistance * KMaxAmDistance) * Y * (Y - 2 * KMaxAmDistance))
            //一个开口向下,顶点(SCROLLDISTANCE,1),过(0,0),(2*SCROLLDISTANCE,0)的二次函数
            let factorAngle = abs( -4 / (KMaxAmDistance * KMaxAmDistance) * Y * (Y - KMaxAmDistance))
            
            // 2.缩小
            var t = CATransform3DIdentity
            t.m34  = 1.0 / -1000
            t = CATransform3DScale(t, 1 - KMaxAmScale * factorScroll, 1 - KMaxAmScale * factorScroll, 0)
            // 3.旋转
            t = CATransform3DRotate(t, factorAngle * KMaxAmAngle, (translation.y > 0) ? -1 : 1, 0, 0)
            _imageView.layer.transform = t
            
            // 4.其他
            self._blurView.alpha = 1 - Y / KMaxAmDistance
        case .ended,
             .cancelled:
            
            UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                
                self._imageView.center = self._originPoint
                self._imageView.layer.transform = CATransform3DIdentity
                self._blurView.alpha = 1
            }, completion: { (_) in
            })
        default:
            break
        }
    }
}




